package logic

import (
	"context"
	"encoding/json"
	"renting-demo/houseorder_srv/houseorder"

	"renting-demo/api/internal/svc"
	"renting-demo/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type OrderListHouseLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewOrderListHouseLogic(ctx context.Context, svcCtx *svc.ServiceContext) *OrderListHouseLogic {
	return &OrderListHouseLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *OrderListHouseLogic) OrderListHouse(req *types.OrderListRequest) (resp *types.Data, err error) {
	// todo: add your logic here and delete this line
	res, _ := l.svcCtx.HouseOrderServer.OrderList(context.Background(), &houseorder.OrderListRequest{
		Userid: int64(req.UserId),
		Page:   int64(req.Page),
		Search: req.Search,
	})
	if len(res.Orders) == 0 {
		return &types.Data{
			Code:    10001,
			Message: "房源订单列表获取失败",
			Data:    nil,
		}, nil
	}
	re, _ := json.Marshal(res.Orders)
	return &types.Data{
		Code:    200,
		Message: "房源订单列表获取成功",
		Data: map[string]string{
			"search": res.Search,
			"data":   string(re),
		},
	}, nil
}
